package com.sky.com.sky.web.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param categoryDTO
     * @return
     * **/
    @GetMapping("/page")
    public Result getPage(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult<Category> pageResult = categoryService.getPate(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 添加分类
     * @param categoryDTO
     * @return
     * **/
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 根据ID删除分类
     * @param id
     * @return
     * **/
    @DeleteMapping
    public Result deleteById(Long id){
        categoryService.deleteById(id);
        return Result.success();
    }



    /**
     * 根据id修改分类
     * @param category
     * @return
     * **/
    @PutMapping
    public Result update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success();
    }



    /**
     * 分类启用禁用
     * @param status
     * @param id
     * @return
     * **/
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status,
                              @RequestParam Long id){
        categoryService.startOrStop(status,id);
        return Result.success();
    }


    /**
     * 根据type值查询分类列表
     * @param type
     * @return
     * **/
    @GetMapping("/list")
    public Result getList(Integer type){
        List<Category> list = categoryService.getList(type);
        return Result.success(list);
    }


}
